package com.vhall.component.controller.filter;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ReUtil;
import com.vhall.component.controller.filter.facade.FilterWordsConsoleFacade;
import com.vhall.component.entity.filter.vo.*;
import com.vhall.component.entity.page.vo.PageVO;
import com.vhall.component.framework.common.annotation.VhallComponent;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.annotation.TokenAuth;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.plugin.common.support.Token;
import com.vhall.component.plugin.common.support.TokenThreadLocal;
import com.vhall.component.service.filter.FilterWordsConsoleService;
import com.vhall.component.service.filter.constant.FilterConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;


/**
 * @author longzhen.gong
 * @date 2021/7/2 20:07
 */
@TokenAuth
@VhallComponent
public class FilterWordsConsoleController implements FilterWordsConsoleFacade {

    @Autowired
    private FilterWordsConsoleService filterWordsConsoleService;

    private static final List<String> EXT_NAME_LIST = Arrays.asList("xlsx", "xls", "csv");


    @Override
    public PageVO<FilterWordsListRspVO> list(FilterWordsListReqVO reqVO) {
        Token token = TokenThreadLocal.getToken();
        if (token == null) {
            throw new BusinessException(BizErrorCode.AUTH_LOGIN_NEEDS);
        }
        Integer accountId = token.getAccountId();
        return filterWordsConsoleService.list(reqVO, accountId);
    }

    @Override
    public FilterWordsCreateRspVO create(FilterWordsCreateReqVO reqVo) {
        Token token = TokenThreadLocal.getToken();
        if (token == null) {
            throw new BusinessException(BizErrorCode.AUTH_LOGIN_NEEDS);
        }
        Integer accountId = token.getAccountId();
        filterWordsConsoleService.create(StringUtils.isBlank(reqVo.getRoomId()) ? null : reqVo.getRoomId(), reqVo.getKeyword(), accountId, accountId);
        return null;
    }

    @Override
    public Boolean update(FilterWordsUpdateReqVO repVO) {
        Token token = TokenThreadLocal.getToken();
        if (token == null) {
            throw new BusinessException(BizErrorCode.AUTH_LOGIN_NEEDS);
        }
        Integer accountId = token.getAccountId();
        filterWordsConsoleService.update(repVO.getId(), repVO.getKeyword(), StringUtils.isBlank(repVO.getRoomId()) ? null : repVO.getRoomId(), accountId);
        return null;
    }


    @Override
    public Integer delete(FilterWordsDeleteReqVO reqVo) {
        if (!ReUtil.isMatch(FilterConstant.REG_IDS, reqVo.getIds())) {
            throw new BusinessException(BizErrorCode.TYPE_INVALID_STRING);
        }
        return filterWordsConsoleService.delete(reqVo.getIds());
    }

    @Override
    public FilterWordsImportRspVO importData(FilterWordsConsoleImportVO filterWordsConsoleImportVo) {
        String fileName = filterWordsConsoleImportVo.getFile().getOriginalFilename();
        String extName = FileUtil.extName(fileName);
        if (!EXT_NAME_LIST.contains(extName)) {
            throw new BusinessException(BizErrorCode.TYPE_FILE_INVALID);
        }
        Token token = TokenThreadLocal.getToken();
        if (token == null) {
            throw new BusinessException(BizErrorCode.AUTH_LOGIN_NEEDS);
        }
        Integer accountId = token.getAccountId();

        return filterWordsConsoleService.importFile(filterWordsConsoleImportVo.getRoomId(), filterWordsConsoleImportVo.getFile(), accountId);
    }


    @Override
    public Boolean exportSendFilterWords(FilterWordsConsoleExportVO reqVo) {
        Token token = TokenThreadLocal.getToken();
        if (token == null) {
            throw new BusinessException(BizErrorCode.AUTH_LOGIN_NEEDS);
        }
        Integer accountId = token.getAccountId();

        String now = LocalDateTimeUtil.format(LocalDateTime.now(), DatePattern.PURE_DATETIME_PATTERN);

        String fileName = "FilterWordSendList" + now + accountId;
        return filterWordsConsoleService.export(reqVo.getRoomId(), accountId, fileName, reqVo.getBeginTime(), reqVo.getEndTime());
    }

    @Override
    public String getFilterWords(FilterWordsGetReqVO vo) {
        return filterWordsConsoleService.getFilterWords(vo);
    }

    @Override
    public Boolean reportFilterWords(FilterWordsReportReqVO reqVO) {
        Token token = TokenThreadLocal.getToken();
        Integer accountId = token.getAccountId();
        if (accountId == null) {
            throw new BusinessException(BizErrorCode.AUTH_LOGIN_NEEDS);
        }
        return filterWordsConsoleService.reportFilterWords(reqVO, accountId);
    }


}
